<?php
class PageRevisionDropDownList extends CInputWidget
{
    public $pageId;
    private $_data;
    
    public function init()
    {
        parent::init();
        if (!$this->pageId)
            throw new CException('Required PageId');
        
        if (is_null($this->_data))
        {
            $this->_data = FSM::_run('Cms.PageRevision.getAvailableRevisions',array('page_id' => $this->pageId))->revisions;
        }
        
        $currentRevision = 0;
        if ($layout = Yii::app()->request->getParam('layout', false))
            $currentRevision = FSM::run('Cms.pageRevision.getCurrent', array('page_id'=>$this->pageId))->revision;
        if (is_object($this->model))
            $this->model->{$this->attribute} = $currentRevision;
        else
            $this->value = $currentRevision;
    }
    
    public function run()
    {
        if (is_object($this->model))
            echo CHtml::activeDropDownList($this->model, $this->attribute, $this->getData(), $this->htmlOptions);
        else
            echo CHtml::dropDownList($this->name, $this->value, $this->getData(), $this->htmlOptions);
    }
    
    public function getData()
    {
        return $this->_data;
    }
}